﻿using System;
namespace hconnect {
    /**
   *┌────────────────────────────────────────────────┐
   *│　描    述：SiemensUtil                                                    
   *│　作    者：sgj                                              
   *│　版    本：1.0                                              
   *│　创建时间：2021/3/25 15:09:23                        
   *└────────────────────────────────────────────────┘
   **/
    public class S7ByteUtil {
        public static int int32(byte[] b ,int begin) {
            if( b == null ) {
                return 0;
            }
            byte[] a = new byte[]
                  {
                            b[begin+3],
                            b[begin+2],
                            b[begin+1],
                            b[begin]
                  };
            return BitConverter.ToInt32(a ,0);
        }

        public static int simotionInt32(byte[] b ,int begin) {
            if( b == null ) {
                return 0;
            }
            byte[] a = new byte[]
                  {
                           b[begin],
                           b[begin+1],
                           b[begin+2],
                           b[begin+3]
                  };
            return BitConverter.ToInt32(a ,0);
        }
        public static float simotionFloat32(byte[] b ,int begin) {
            if( b == null ) {
                return 0;
            }
            byte[] a = new byte[]
                  {
                           b[begin],
                           b[begin+1],
                           b[begin+2],
                           b[begin+3]
                  };
            return BitConverter.ToSingle(a ,0);
        }
        public static ushort ushort16(byte[] b ,int begin) {
            if( b == null ) {
                return 0;
            }
            byte[] a = new byte[]
                  {
                            b[begin+1],
                            b[begin]
                  };
            return BitConverter.ToUInt16(a ,0);
        }
        public static float toFloat(byte[] b ,int begin) {
            if( b == null ) {
                return 0;
            }
            byte[] a = new byte[]
                  {         b[begin+3],
                            b[begin+2],
                            b[begin+1],
                            b[begin]
                  };
            return BitConverter.ToSingle(a ,0);
        }
        public static ushort getByte(byte[] b ,int begin) {
            if( b == null ) {
                return 0;
            }

            return b[begin];
        }

        static public void margnS7L2(byte[] array ,int index ,int value) {
            byte[] b = BitConverter.GetBytes(value);

            array[index] = b[1];
            array[index + 1] = b[0];
        }

        static public void margnS7L4(byte[] array ,int index ,int value) {
            byte[] b = BitConverter.GetBytes(value);

            array[index] = b[3];
            array[index + 1] = b[2];
            array[index + 2] = b[1];
            array[index + 3] = b[0];
        }
        static public byte[] toS7L2(int value) {
            byte[] b = BitConverter.GetBytes(value);
            return new byte[] { b[1] ,b[0] };
        }
        static public byte[] toS7L4(int value) {
            byte[] b = BitConverter.GetBytes(value);
            return new byte[] { b[3] ,b[2] ,b[1] ,b[0] };
        }
        public static String toString(byte[] b ,int begin ,int len) {
            byte[] bytes = getByte(b ,begin ,len);
            return toString(bytes);
        }
        public static String toString(byte[] b) {
            int k = 0;
            for( ;k < b.Length;k++ ) {
                if( b[k] == 0 ) {
                    break;
                }
            }
            byte[] r = getByte(b ,0 ,k);
            String user = System.Text.Encoding.Default.GetString(r);
            return user;

        }
        public static byte[] getByte(byte[] statebyte ,int begin ,int len) {
            byte[] bytes = new byte[len];
            for( int i = begin;i < len + begin;i++ ) {
                bytes[i - begin] = statebyte[i];
            }
            return bytes;
        }

    }
}
